<?php
namespace Store\Controller;
use Think\Image;

use Think\Page;

use Think\Controller;
class ScenicEntertainmentController extends  Controller{
	public function index(){

		$ticket = D("ScenicTicket");
		$userid = $_SESSION['uid'];
		$scenic = M('Scenic')->where(array('user_id'=>$userid))->field('id')->select();
		$where = array();
		foreach ($scenic as $k=>$v){
			array_push($where,$v['id']);
		}
		if(sizeof($where)>0){
			$w['scenic_id']=array("in",$where);
		    $w['type']     =array("eq", 1);
			$count = $ticket->where($w)->count();
			$page = new Page($count,4);
			$page->setConfig('prev', '上页');
			$page->setConfig('next', '下页');
			$show = $page->show();
			$list = $ticket->relation(true)->where($w)->limit($page->firstRow,$page->listRows)->select();
		}
		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('page',$show);
		$this->display();
		
	}
	public function add(){
		if(IS_POST){
			$file = $_FILES['image'];
			$uploadpath = "Public/Upload/Ticket/";//上传路径
	
			$util = A('Util');
			$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建
	
			$pinfo = pathinfo($file['name']);
			$ftype = $pinfo['extension'];//文件类型
			$name = basename($file['name'],".{$ftype}");
	
			$fileName = md5($name.time()).".".$ftype;//识别图
			$image = new Image();
			$image->open($file['tmp_name']);
			$image->thumb('100', '100')->save($uploadpath.$fileName);
	
			$data = $_POST;
			$ticket = D("ScenicTicket");
			$data["image"] = 'http://qianmosen.cn/tubu/'.$uploadpath.$fileName;
			$data["type"] = 1;
			
			$data = $ticket->create($data);
			$result= $ticket->add($data);
			if($result){
				$this->success("添加成功","index");
			}else{
				$this->error("添加失败");
			}
		}else{
			$scenic = D('Scenic');
			$list = $scenic->where(array('user_id'=>$_SESSION['uid']))->select();
			$this->assign("list",$list);
			$this->display();
		}
	}
	
	public function delete(){
		$where['id'] = $_GET['id'];
		$ticket = D('ScenicEntertainment');
		$list = $ticket->where($where)->find();
		$list['image'] = substr($list['image'], 25);
		if(is_file($list['image'])){
			unlink($list['image']);
		}
		$result = $ticket->where($where)->delete();
		if($result){
			$this->success("删除成功","index");
		}else{
			$this->error("删除失败");
		}
	}
	
}